#################################################################################
# Filename: 2_round1_appendices.R
# Description: Takes the cleaned data from both rounds and creates plots of
# key preregistered outcomes displayed in Appendix A
#
# Note: you must have run both scripts prefixed 1_ before running this script.
#################################################################################
rm(list=ls())


### Load library dependencies 
library(tidyverse)
library(broom)
library(DeclareDesign)
library(estimatr)
library(rio)
library(ggplot2)
library(gridExtra)
library(interflex)
library(stargazer)


#GG Plot theme
theme_bw1 <- function(base_size = 16, base_family = "") {
  theme_bw(base_size = base_size, base_family = base_family) %+replace%
    theme(
      axis.text.x =       element_text(size = base_size, colour = "black",  hjust = .5 , vjust=1),
      axis.text.y =       element_text(size = base_size , colour = "black", hjust = 0 , vjust=.5 ), # changes position of X axis text
      axis.ticks =        element_line(colour = "grey50"),
      #   axis.title.y =      element_text(size = base_size,angle=90,vjust=.01,hjust=.1),
      legend.position = "none"
    )
}



# Import cleaned data from both rounds
dat1 <- import("Round_1_clean.Rdata") 
dat2 <- import("Round_2_clean.Rdata") 


### CREATE PLOTS IN APPENDIX A ###

test1 <- t.test(dat1$patriot_pca[dat1$clip=="leopard"], dat1$patriot_pca[dat1$clip=="none"])
test2 <- t.test(dat1$patriot_pca[dat1$clip=="yongbu"], dat1$patriot_pca[dat1$clip=="none"])

test3 <- t.test(dat1$antiforeign_pca[dat1$clip=="leopard"], dat1$antiforeign_pca[dat1$clip=="none"])
test4 <- t.test(dat1$antiforeign_pca[dat1$clip=="yongbu"], dat1$antiforeign_pca[dat1$clip=="none"])

test5 <- t.test(dat1$militant_pca[dat1$clip=="leopard"], dat1$militant_pca[dat1$clip=="none"])
test6 <- t.test(dat1$militant_pca[dat1$clip=="yongbu"], dat1$militant_pca[dat1$clip=="none"])

test7 <- t.test(dat1$protest.gov[dat1$clip=="leopard"], dat1$protest.gov[dat1$clip=="none"])
test8 <- t.test(dat1$protest.gov[dat1$clip=="yongbu"], dat1$protest.gov[dat1$clip=="none"])

test9 <- t.test(dat1$Q19_1[dat1$clip=="leopard"], dat1$Q19_1[dat1$clip=="none"])
test10 <- t.test(dat1$Q19_1[dat1$clip=="yongbu"], dat1$Q19_1[dat1$clip=="none"])





test13 <- t.test(dat2$patriot_pca[dat2$clip=="Japan"], dat2$patriot_pca[dat2$clip=="placebo"])
test14 <- t.test(dat2$patriot_pca[dat2$clip=="HK"], dat2$patriot_pca[dat2$clip=="placebo"])

test15 <- t.test(dat2$antiforeign_japan_pca[dat2$clip=="Japan"], dat2$antiforeign_japan_pca[dat2$clip=="placebo"])
test16 <- t.test(dat2$antiforeign_usa_pca[dat2$clip=="HK"], dat2$antiforeign_usa_pca[dat2$clip=="placebo"])

test17 <- t.test(dat2$militant_pca[dat2$clip=="Japan"], dat2$militant_pca[dat2$clip=="placebo"])
test18 <- t.test(dat2$militant_pca[dat2$clip=="HK"], dat2$militant_pca[dat2$clip=="placebo"])

test19 <- t.test(dat2$protest.gov[dat2$clip=="Japan"], dat2$protest.gov[dat2$clip=="placebo"])
test20 <- t.test(dat2$protest.gov[dat2$clip=="HK"], dat2$protest.gov[dat2$clip=="placebo"])

test21 <- t.test(dat2$Q19_1[dat2$clip=="Japan"], dat2$Q19_1[dat2$clip=="placebo"])
test22 <- t.test(dat2$Q19_1[dat2$clip=="HK"], dat2$Q19_1[dat2$clip=="placebo"])



test25 <- t.test(dat2$patriot_pca_f[dat2$clip=="Japan"], dat2$patriot_pca_f[dat2$clip=="placebo"])
test26 <- t.test(dat2$patriot_pca_f[dat2$clip=="HK"], dat2$patriot_pca_f[dat2$clip=="placebo"])

test27 <- t.test(dat2$antiforeign_japan_pca_f[dat2$clip=="Japan"], dat2$antiforeign_japan_pca_f[dat2$clip=="placebo"])
test28 <- t.test(dat2$antiforeign_usa_pca_f[dat2$clip=="HK"], dat2$antiforeign_usa_pca_f[dat2$clip=="placebo"])

test29 <- t.test(dat2$militant_pca_f[dat2$clip=="Japan"], dat2$militant_pca_f[dat2$clip=="placebo"])
test30 <- t.test(dat2$militant_pca_f[dat2$clip=="HK"], dat2$militant_pca_f[dat2$clip=="placebo"])

test31 <- t.test(dat2$protest.gov_f[dat2$clip=="Japan"], dat2$protest.gov_f[dat2$clip=="placebo"])
test32 <- t.test(dat2$protest.gov_f[dat2$clip=="HK"], dat2$protest.gov_f[dat2$clip=="placebo"])

test33 <- t.test(dat2$Q19_1_f[dat2$clip=="Japan"], dat2$Q19_1_f[dat2$clip=="placebo"])
test34 <- t.test(dat2$Q19_1_f[dat2$clip=="HK"], dat2$Q19_1_f[dat2$clip=="placebo"])








estimates1 <- data.frame(pe=c(test2$estimate[1]-test2$estimate[2],
                              test1$estimate[1]-test1$estimate[2],
                              test13$estimate[1]-test13$estimate[2],
                              test14$estimate[1]-test14$estimate[2],
                              test25$estimate[1]-test25$estimate[2],
                              test26$estimate[1]-test26$estimate[2]),
                         se.high=c(test2$conf.int[2],
                                   test1$conf.int[2],
                                   test13$conf.int[2],
                                   test14$conf.int[2],
                                   test25$conf.int[2],
                                   test26$conf.int[2]),
                         se.low=c(test2$conf.int[1],
                                  test1$conf.int[1],
                                  test13$conf.int[1],
                                  test14$conf.int[1],
                                  test25$conf.int[1],
                                  test26$conf.int[1]),
                         names=c("News Broadcast\nRound 1", "Television Drama\nRound 1", "Television Drama\nRound 2", "Social Media\nRound 2", "Television Drama\nR2 Follow-Up", "Social Media\nR2 Follow-Up")
)

estimates1$names <- fct_relevel(estimates1$names,"News Broadcast\nRound 1", "Television Drama\nRound 1", "Television Drama\nRound 2", "Social Media\nRound 2", "Television Drama\nR2 Follow-Up", "Social Media\nR2 Follow-Up")
?pdf
pdf("aggregate_patriotism.pdf", width=13, height = 4)
p1 = ggplot(estimates1, aes(y=pe, x=names))
p1 = p1 + geom_hline(yintercept = 0,size=1,colour="black",linetype="dotted") 
p1 = p1 + geom_pointrange(aes(ymin=se.high,ymax=se.low),position="dodge",size=1.4, col="blue")
p1 = p1 + scale_y_continuous(name="Outcome: Patriotism",  limits=c(-0.5, 0.5)) 
p1 = p1 + scale_x_discrete(name="") 
p1 = p1  + theme_bw1()
p1
dev.off()





estimates2 <- data.frame(pe=c(test4$estimate[1]-test4$estimate[2],
                              test3$estimate[1]-test3$estimate[2],
                              test15$estimate[1]-test15$estimate[2],
                              test16$estimate[1]-test16$estimate[2],
                              test27$estimate[1]-test27$estimate[2],
                              test28$estimate[1]-test28$estimate[2]),
                         se.high=c(test4$conf.int[2],
                                   test3$conf.int[2],
                                   test15$conf.int[2],
                                   test16$conf.int[2],
                                   test27$conf.int[2],
                                   test28$conf.int[2]),
                         se.low=c(test4$conf.int[1],
                                  test3$conf.int[1],
                                  test15$conf.int[1],
                                  test16$conf.int[1],
                                  test27$conf.int[1],
                                  test28$conf.int[1]),
                         names=c("News Broadcast\nRound 1", "Television Drama\nRound 1", "Television Drama\nRound 2", "Social Media\nRound 2", "Television Drama\nR2 Follow-Up", "Social Media\nR2 Follow-Up")
)

estimates2$names <- fct_relevel(estimates1$names,"News Broadcast\nRound 1", "Television Drama\nRound 1", "Television Drama\nRound 2", "Social Media\nRound 2", "Television Drama\nR2 Follow-Up", "Social Media\nR2 Follow-Up")

pdf("aggregate_antiforeign.pdf", width=13, height = 4)
p1 = ggplot(estimates2, aes(y=pe, x=names))
p1 = p1 + geom_hline(yintercept = 0,size=1,colour="black",linetype="dotted") 
p1 = p1 + geom_pointrange(aes(ymin=se.high,ymax=se.low),position="dodge",size=1.4, col="blue")
p1 = p1 + scale_y_continuous(name="Outcome: Anti-Foreign Nationalism",  limits=c(-0.5, 0.5)) 
p1 = p1 + scale_x_discrete(name="") 
p1 = p1  + theme_bw1()
p1
dev.off()



estimates3 <- data.frame(pe=c(test6$estimate[1]-test6$estimate[2],
                              test5$estimate[1]-test5$estimate[2],
                              test17$estimate[1]-test17$estimate[2],
                              test18$estimate[1]-test18$estimate[2],
                              test29$estimate[1]-test29$estimate[2],
                              test30$estimate[1]-test30$estimate[2]),
                         se.high=c(test6$conf.int[2],
                                   test5$conf.int[2],
                                   test17$conf.int[2],
                                   test18$conf.int[2],
                                   test29$conf.int[2],
                                   test30$conf.int[2]),
                         se.low=c(test6$conf.int[1],
                                  test5$conf.int[1],
                                  test17$conf.int[1],
                                  test18$conf.int[1],
                                  test29$conf.int[1],
                                  test30$conf.int[1]),
                         names=c("News Broadcast\nRound 1", "Television Drama\nRound 1", "Television Drama\nRound 2", "Social Media\nRound 2", "Television Drama\nR2 Follow-Up", "Social Media\nR2 Follow-Up")
)

estimates3$names <- fct_relevel(estimates1$names,"News Broadcast\nRound 1", "Television Drama\nRound 1", "Television Drama\nRound 2", "Social Media\nRound 2", "Television Drama\nR2 Follow-Up", "Social Media\nR2 Follow-Up")

pdf("aggregate_hawkish.pdf", width=13, height = 4)
p1 = ggplot(estimates3, aes(y=pe, x=names))
p1 = p1 + geom_hline(yintercept = 0,size=1,colour="black",linetype="dotted") 
p1 = p1 + geom_pointrange(aes(ymin=se.high,ymax=se.low),position="dodge",size=1.4, col="blue")
p1 = p1 + scale_y_continuous(name="Outcome: Hawkishness",  limits=c(-0.5, 0.5)) 
p1 = p1 + scale_x_discrete(name="") 
p1 = p1  + theme_bw1()
p1
dev.off()




estimates4 <- data.frame(pe=c(test8$estimate[1]-test8$estimate[2],
                              test7$estimate[1]-test7$estimate[2],
                              test19$estimate[1]-test19$estimate[2],
                              test20$estimate[1]-test20$estimate[2],
                              test31$estimate[1]-test31$estimate[2],
                              test32$estimate[1]-test32$estimate[2]),
                         se.high=c(test8$conf.int[2],
                                   test7$conf.int[2],
                                   test19$conf.int[2],
                                   test20$conf.int[2],
                                   test31$conf.int[2],
                                   test32$conf.int[2]),
                         se.low=c(test8$conf.int[1],
                                  test7$conf.int[1],
                                  test19$conf.int[1],
                                  test20$conf.int[1],
                                  test31$conf.int[1],
                                  test32$conf.int[1]),
                         names=c("News Broadcast\nRound 1", "Television Drama\nRound 1", "Television Drama\nRound 2", "Social Media\nRound 2", "Television Drama\nR2 Follow-Up", "Social Media\nR2 Follow-Up")
)

estimates4$names <- fct_relevel(estimates1$names,"News Broadcast\nRound 1", "Television Drama\nRound 1", "Television Drama\nRound 2", "Social Media\nRound 2", "Television Drama\nR2 Follow-Up", "Social Media\nR2 Follow-Up")

pdf("aggregate_protest.pdf", width=13, height = 4)
p1 = ggplot(estimates4, aes(y=pe, x=names))
p1 = p1 + geom_hline(yintercept = 0,size=1,colour="black",linetype="dotted") 
p1 = p1 + geom_pointrange(aes(ymin=se.high,ymax=se.low),position="dodge",size=1.4, col="blue")
p1 = p1 + scale_y_continuous(name="Outcome: Protest Government",  limits=c(-0.25, 0.25)) 
p1 = p1 + scale_x_discrete(name="") 
p1 = p1  + theme_bw1()
p1
dev.off()



estimates5 <- data.frame(pe=c(test10$estimate[1]-test10$estimate[2],
                              test9$estimate[1]-test9$estimate[2],
                              test21$estimate[1]-test21$estimate[2],
                              test22$estimate[1]-test22$estimate[2],
                              test33$estimate[1]-test33$estimate[2],
                              test34$estimate[1]-test34$estimate[2]),
                         se.high=c(test10$conf.int[2],
                                   test9$conf.int[2],
                                   test21$conf.int[2],
                                   test22$conf.int[2],
                                   test33$conf.int[2],
                                   test34$conf.int[2]),
                         se.low=c(test10$conf.int[1],
                                  test9$conf.int[1],
                                  test21$conf.int[1],
                                  test22$conf.int[1],
                                  test33$conf.int[1],
                                  test34$conf.int[1]),
                         names=c("News Broadcast\nRound 1", "Television Drama\nRound 1", "Television Drama\nRound 2", "Social Media\nRound 2", "Television Drama\nR2 Follow-Up", "Social Media\nR2 Follow-Up")
)

estimates5$names <- fct_relevel(estimates1$names,"News Broadcast\nRound 1", "Television Drama\nRound 1", "Television Drama\nRound 2", "Social Media\nRound 2", "Television Drama\nR2 Follow-Up", "Social Media\nR2 Follow-Up")

pdf("aggregate_anger.pdf", width=13, height = 4)
p1 = ggplot(estimates5, aes(y=pe, x=names))
p1 = p1 + geom_hline(yintercept = 0,size=1,colour="black",linetype="dotted") 
p1 = p1 + geom_pointrange(aes(ymin=se.high,ymax=se.low),position="dodge",size=1.4, col="blue")
p1 = p1 + scale_y_continuous(name="Outcome: Anger",  limits=c(-0.5,2)) 
p1 = p1 + scale_x_discrete(name="") 
p1 = p1  + theme_bw1()
p1
dev.off()




